<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://www.joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		GNU GPL v3
*/


defined('_JEXEC') or die;
define('DANIPRO_PRO_THUMB_WIDTH',200);
define('DANIPRO_PRO_THUMB_HEIGHT',200);
define('DANIPRO_PRO_LARGE_WIDTH',800);
define('DANIPRO_PRO_LARGE_HEIGHT',800);
define('DANIPRO_CAT_THUMB_WIDTH',200);
define('DANIPRO_CAT_THUMB_HEIGHT',200);
define('DANIPRO_CAT_LARGE_WIDTH',900);
define('DANIPRO_CAT_LARGE_HEIGHT',900);


class DaniProHelper
{
	var $arr_unit = array(
		array('value'=>'1','text'=>'Bao'),
		array('value'=>'2','text'=>'Phuy'),
		array('value'=>'3','text'=>'Can'),
		array('value'=>'4','text'=>'Thùng'),
		array('value'=>'5','text'=>'Bịch'),
		array('value'=>'6','text'=>'Hộp'),
		array('value'=>'7','text'=>'Gói'),
	
	);
	
	public static function addSubmenu($vName)
	{
		JSubMenuHelper::addEntry(
			JText::_('COM_DANIPRO_SUBMENU_DANIPRO'),
			'index.php?option=com_danipro&view=danipro',
			$vName == 'danipro'
		);
	
		JSubMenuHelper::addEntry(
			JText::_("COM_DANIPRO_SUBMENU_CATEGORIES"),
			"index.php?option=com_danipro&view=categories",
			$vName == "categories"
		);
		JSubMenuHelper::addEntry(
			JText::_("COM_DANIPRO_SUBMENU_PRODUCTS"),
			"index.php?option=com_danipro&view=products",
			$vName == "products"
		);
		JSubMenuHelper::addEntry(
			JText::_("COM_DANIPRO_SUBMENU_SUPPLIERS"),
			"index.php?option=com_danipro&view=suppliers",
			$vName == "suppliers"
		);
		JSubMenuHelper::addEntry(
			JText::_("COM_DANIPRO_SUBMENU_MEDIAS"),
			"index.php?option=com_danipro&view=medias",
			$vName == "medias"
		);
	}
	/**
	 * Gets a list of the actions that can be performed.
	 * @return	JObject
	 * @since	1.6
	 */

	public static function getActions()
	{
		$user	= JFactory::getUser();
		$result	= new JObject;
		$assetName = 'com_danipro';
		$actions = array(
			'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.state', 'core.delete'
		);
		foreach ($actions as $action) {
			$result->set($action,	$user->authorise($action, $assetName));

		}
		return $result;

	}
	function quickIconButton( $link, $image, $text ) {
		
		$lang	= &JFactory::getLanguage();
		$button = '';
		$button .= '<div class="icon-wrapper">';
		$button .=	'<div class="icon">'
				   .'<a href="'.$link.'">'
				   .JHTML::_('image', 'administrator/components/com_danipro/assets/images/'.$image, $text )
				   .'<span>'.$text.'</span></a>'
				   .'</div>';
		$button .= '</div>';
		return $button;
	}
	function treerecurse( $id, $indent, $list, &$children, $maxlevel=9999, $level=0, $type=0 )
	{

		if (@$children[$id] && $level <= $maxlevel)
		{
			foreach ($children[$id] as $v)
			{
				$id = $v->id;

				if ( $type ) {
					$pre 	= '<sup>|_</sup>&nbsp;';
					$spacer = '.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				} else {
					$pre 	= '&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;';
					$spacer = '&nbsp;&nbsp;';
				}

				if ( $v->parent == 0 ) {
					$txt 	= $v->title;
				} else {
					$txt 	= $pre . $v->title;
				}
				$pt = $v->parent;
				$list[$id] = $v;
				$list[$id]->treename = "$indent$txt";
				$list[$id]->text = "$indent$txt";				
				$list[$id]->children = count( @$children[$id] );
				$list = DaniProHelper::treerecurse( $id, $indent . $spacer, $list, $children, $maxlevel, $level+1, $type );
			}
		}
		return $list;
	}

	function getPackageList()
	{
		return 	array(
		array('value'=>'1','text'=>'Bao'),
		array('value'=>'2','text'=>'Phuy'),
		array('value'=>'3','text'=>'Can'),
		array('value'=>'4','text'=>'Thùng'),
		array('value'=>'5','text'=>'Bịch'),
		array('value'=>'6','text'=>'Hộp'),
		array('value'=>'7','text'=>'Gói'),	
		);
	}
	function getPackageName($key)
	{
		$list = array(
		array('value'=>'1','text'=>'Bao'),
		array('value'=>'2','text'=>'Phuy'),
		array('value'=>'3','text'=>'Can'),
		array('value'=>'4','text'=>'Thùng'),
		array('value'=>'5','text'=>'Bịch'),
		array('value'=>'6','text'=>'Hộp'),
		array('value'=>'7','text'=>'Gói'),	
		);
		
		return $list[$key-1]['text'];
	}
	function getImageName()
	{
		$data = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h');
		$txt = "";
		for($i=0;$i<16;$i++)
			$txt.=$data[rand(0,17)];
		return date('Ymd').time().$txt;
	}
	function getCategoryList()
	{
		$db = & JFactory::getDBO();
		$first[]  = array('value'=>0,'text'=>JText::_("COM_DANIPRO_SELECT_CATEGORY"));
		$id = JRequest::getVar('id');
		$db->setQuery("select id,title,id as value,title as text,parent from #__dani_categories where published=1 order by title");
		$rows = $db->loadObjectList();
		
		$levellimit = 100;
		$children = array();
		// first pass - collect children
		foreach ($rows as $v )
		{
			$pt = $v->parent;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push( $list, $v );
			$children[$pt] = $list;
		}

		// second pass - get an indent list of the items
		$list = DaniProHelper::treerecurse(0, '', array(), $children, max( 0, $levellimit-1 ));
		
		if($list)
			$options  = array_merge($first,$list);
		else
			$options = $first;
		return JHTML::_("select.genericlist",$options,'category','style="width:200px;" onchange="document.adminForm.submit()"','value','text',JRequest::getVar('category'));
	}
	function getSupplierList()
	{
		$db = & JFactory::getDBO();
		$first[]  = array('value'=>0,'text'=>JText::_("COM_DANIPRO_SELECT_SUPPLIER"));
		$db->setQuery("select id as value,title as text from #__dani_supplier where published=1 order by title");
		$rows = $db->loadObjectList();
		if($rows)
			$options  = array_merge($first,$rows);
		else
			$options = $first;
		return JHTML::_("select.genericlist",$options,'supplier','style="width:200px;" onchange="document.adminForm.submit()"','value','text',JRequest::getVar('supplier'));
		
	}
	function getCountryList()
	{
		$db = & JFactory::getDBO();
		$first[]  = array('value'=>0,'text'=>JText::_("COM_DANIPRO_SELECT_COUNTRY"));
		$db->setQuery("select id as value,name as text from #__dani_countries where published=1 order by name");
		$rows = $db->loadObjectList();
		if($rows)
			$options  = array_merge($first,$rows);
		else
			$options = $first;
		return JHTML::_("select.genericlist",$options,'madein','style="width:200px;" onchange="document.adminForm.submit()"','value','text',JRequest::getVar('madein'));
	}
	function getProductList()
	{
		$db = & JFactory::getDBO();
		$first[]  = array('value'=>0,'text'=>JText::_("COM_DANIPRO_SELECT_PRODUCT"));
		$db->setQuery("select id as value,title as text from #__dani_products order by title");
		$rows = $db->loadObjectList();
		if($rows)
			$options  = array_merge($first,$rows);
		else
			$options = $first;
		return JHTML::_("select.genericlist",$options,'pid','style="width:200px;" onchange="document.adminForm.submit()"','value','text',JRequest::getVar('pid'));
	}
	function formatPrice($price)
	{
		return number_format($price,2,',','.'). ' VND';
	}	
}

